#include"io.h"
#include"hd.h"
#include"sched.h"
#include"types.h"
#include"system.h"
#include"syscall.h"
#include"processor.h"
#include<stdio.h>
extern int printk(char *fmtstr, ...);
#define	KRNL_STACK_SIZE	4096
char krnlStack[KRNL_STACK_SIZE];


System system;

void ring3();
int KernelEntry()
{
	unsigned int memSize= 0;
	int i;

	asm("cli");
	//Change Stack...
	asm("movl $krnlStack,%%esp;addl %%eax,%%esp"::"a"(KRNL_STACK_SIZE));

//	printk("Chang privilege to level 3...   \n");
	asm("sti");

	//extern void ParseKbdInput();
	//while(1) ParseKbdInput();
	/* Jump To RING 3 */
	asm("	movl	$0x23,%eax;	\
		movw	%ax,%ds;	\
		movw	%ax,%es;	\
		movw	%ax,%fs;	\
		movw	%ax,%gs;	\
		movl	%esp,%ebp;	\
		pushl	$0x23;		\
		pushl	%ebp;		\
		pushf;			\
		pushl	$0x1B;		\
		leal	ring3,%eax;	\
		pushl	%eax;		\
		iret");
	return 0;
}

void ring3()
{
	int a=0,b=0;
/*
	printk("success.\n");
	printk("test system call...   ");
	asm("xor %eax,%eax");
	SYSENTER
	asm("":"=a"(a));
	printk("returned value: %x\n", a);
*/
	//asm("xor %eax,%eax");
	//while(1)sysenter(0);
	//while(1);
	//sysenter(1);
#if 0
	while(1)
	{
		a = printf("Syscall ...%d ", b);
		b = printf("Syscall ...%d\n", a);
	}
#endif
	while(1)
	{
		printf("s");
		a = 100000;
		while(a--);
	}
}


